package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
            PageHelper.startPage(currentPage,pageSize);
            Page page = userDao.findPage(queryString);
            long total = page.getTotal();
            List rows = page.getResult();
            return new PageResult(total, rows);
    }

    public User findByUsername(String username) {

        // 获取User
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }

        // 获取Role
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);

        // 获取Permission
        if(roles != null && roles.size() > 0){
            for(Role role : roles){
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if(permissions != null && permissions.size() > 0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }

        return user;
    }

    @Override
    public void add(Integer[] roleIds, User user) {
        userDao.add(user);
        Integer userId = user.getId();
        this.setUserAndRole(userId,roleIds);
    }

    @Override
    public User findById(Integer userId) {
        return userDao.findById(userId);
    }

    @Override
    public void update(Integer[] roleIds, User user) {
        this.deleteUserAndRole(user.getId());
        userDao.update(user);
        this.setUserAndRole(user.getId(), roleIds);
    }

    @Override
    public void delete(Integer userId) {
        this.deleteUserAndRole(userId);
        userDao.delete(userId);
    }

    public void deleteUserAndRole(Integer id) {
        userDao.deleteUserAndRole(id);
    }

    public void setUserAndRole(Integer userId, Integer[] roleIds){
        if (roleIds != null && roleIds.length > 0){
            List<Integer> list = new ArrayList<>();
            for (Integer roleId : roleIds) {
                list.add(roleId);
            }
            userDao.setUserAndRole(userId,list);
        }
    }
}